import request, { request as httpRequest } from '@/utils/http/index'

// 会员列表相关API
export interface MemberListParams {
  name?: string
  phone?: string
  labelId?: string
  dates?: string[]
  minMoney?: number
  maxMoney?: number
  minBuyers?: number
  maxBuyers?: number
  page: number
  pageSize: number
}

export interface MemberItem {
  buyerUserId: string
  orderFormid: string
  name: string
  phone: string
  labelName: string
  buyers: number
  money: number
  time: string
  // 新增字段
  remainingPoints?: number  // 剩余积分
  registrationId?: string   // 注册id
  lastLoginIp?: string     // 最后登录ip
  memberLevelName?: string // 会员等级名称
  totalConsumAmount?: number // 消费总额
  totalConsumTimes?: number  // 购买次数
}

export interface MemberListResponse {
  list: MemberItem[]
  total: number
}

// 会员等级相关API
export interface MemberLevelParams {
  memberLevelName?: string
  page: number
  pageSize: number
}

export interface MemberLevelItem {
  memberLevelId: string
  memberLevelName: string
  memberLevelDesc: string
  memberLevelIcon: string
  memberLevelBackground?: string
  memberLevelReason?: string
  growth?: number
  memberIds: string[]
  ids: string[]
}

export interface MemberLevelListResponse {
  list: MemberLevelItem[]
  total: number
}

// 会员权益相关API
export interface MemberEquityParams {
  page: number
  pageSize: number
}

export interface MemberEquityItem {
  memberId: string
  memberName: string
  memberDesc: string
  memberIcon: string
  memberReason: string
}

export interface MemberEquityListResponse {
  list: MemberEquityItem[]
  total: number
}

// 会员标签相关API
export interface LabelItem {
  labelId: string
  labelName: string
}

// 会员tips相关API
export interface TipsItem {
  buyerLabelId?: string
  labelName: string
  labelType: number // 1-手动标签 2-自动标签
  conditions?: string[] // 达标条件
  lastDealTime?: string // 最后消费时间
  dealCount?: number // 累计消费次数
  dealAmount?: number // 累计消费金额
}

// 获取会员列表
export function customerMageGetAll(data: MemberListParams) {
  return httpRequest({
    url: '/buyer/getAll',
    method: 'POST',
    data
  })
}

// 获取所有标签
export function getLabels(data: { name?: string }) {
  return httpRequest({
    url: '/buyer/getLabels',
    method: 'POST',
    data
  })
}

// 添加标签
export function addLabel(data: { buyerUserIds: string[]; buyerLabelIds: string[] }) {
  return httpRequest({
    url: '/buyer/saveUserLabel',
    method: 'POST',
    data
  })
}

// 获取会员详情
export function getMemberDetails(data: { buyerUserId: string }) {
  return httpRequest({
    url: '/buyer/getById',
    method: 'POST',
    data
  })
}

// 获取会员等级列表
export function getMemberlevel(data: MemberLevelParams) {
  return httpRequest({
    url: '/memberlevel/getAll',
    method: 'POST',
    data
  })
}

// 获取会员等级信息
export function getLevelInfo(data: { memberLevelId: string }) {
  return httpRequest({
    url: '/memberlevel/getInfo',
    method: 'POST',
    data
  })
}

// 添加会员等级
export function addLevel(data: MemberLevelItem) {
  return httpRequest({
    url: '/memberlevel/add',
    method: 'POST',
    data
  })
}

// 修改会员等级
export function changeLevel(data: MemberLevelItem) {
  return httpRequest({
    url: '/memberlevel/change',
    method: 'POST',
    data
  })
}

// 删除会员等级
export function deleteLevel(data: { memberLevelId: string }) {
  return httpRequest({
    url: '/memberlevel/delete',
    method: 'POST',
    data
  })
}

// 获取会员权益列表
export function getMembership(data: MemberEquityParams) {
  return httpRequest({
    url: '/membership/getAll',
    method: 'POST',
    data
  })
}

// 获取会员权益信息
export function getMembershipInfo(data: { memberId: string }) {
  return httpRequest({
    url: 'membership/getById',
    method: 'POST',
    data
  })
}

// 添加会员权益
export function addMembership(data: MemberEquityItem) {
  return httpRequest({
    url: '/membership/add',
    method: 'POST',
    data
  })
}

// 修改会员权益
export function changeMembership(data: MemberEquityItem) {
  return httpRequest({
    url: '/membership/change',
    method: 'POST',
    data
  })
}

// 删除会员权益
export function delMembership(data: { memberId: string }) {
  return httpRequest({
    url: '/membership/delete',
    method: 'POST',
    data
  })
}

// 获取所有tips
export function getTips(data: { labelName?: string; labelType?: string; page: number; pageSize: number }) {
  return httpRequest({
    url: '/label/getAll',
    method: 'POST',
    data
  })
}

// 添加tips
export function addTips(data: TipsItem) {
  return httpRequest({
    url: '/label/save',
    method: 'POST',
    data
  })
}

// 更新tips
export function updateTips(data: TipsItem) {
  return httpRequest({
    url: '/label/update',
    method: 'POST',
    data
  })
}

// 删除tips
export function deleteTips(data: { ids: string[] }) {
  return httpRequest({
    url: '/label/delete',
    method: 'POST',
    data
  })
}

// 根据ID获取tips详情
export function getTipsById(data: { buyerLabelId: string }) {
  return httpRequest({
    url: '/label/getById',
    method: 'POST',
    data
  })
}

// 导出标签
export function exportTips(data: { ids: string[] }) {
  const params = new URLSearchParams();
  if (data.ids) {
    data.ids.forEach(id => params.append('ids', id));
  }
  return httpRequest({
    url: '/label/excel_platform_label?' + params.toString(),
    method: 'GET',
    responseType: 'blob'
  });
}